﻿@{
    ViewBag.Title = "城市";
}

<div style="width: 100%;">
    <div class="mini-toolbar" style="border-bottom: 0; padding: 0px;">
        <table style="width: 100%;">
            <tr>
                <td style="width: 100%;">
                    <a id="one" class="mini-button" iconcls="icon-add" plain="true" onclick="addRoot()">插入根节点</a>
                    <a id="two" class="mini-button" iconcls="icon-add" plain="true" onclick="addLeaf()">插入子节点</a>
                    <a id="three" class="mini-button" iconcls="icon-remove" plain="true" onclick="removeNode()">删除</a>
                    <a id="four" class="mini-button" iconcls="icon-reload" plain="true" onclick="reloadRow()">刷新</a>
                    <a id="five" class="mini-button" iconcls="icon-save" plain="true" onclick="saveData()">保存</a>
                </td>
            </tr>
        </table>
    </div>
</div>
<div class="mini-fit" style="height: 100%;">

    <div id="treegrid1" class="mini-treegrid" style="width: 100%; height: 100%;"
        url="Get" showtreeicon="true" showtreelines="true"
        treecolumn="YName" idfield="YID" parentfield="FID" resultastree="false"
        expandonload="true" allowcelledit="true" allowcellselect="true">
        <div property="columns">
            <div type="indexcolumn"></div>
            <div field="YID" visible="false"></div>
            <div field="FID" visible="false"></div>
            <div field="YName" name="YName" width="190px" vtype="required">
                城市名称
                <input property="editor" class="mini-textbox" style="width: 100%;" />
            </div>
            <div field="YCode" width="100%">
                城市编码
                <input property="editor" class="mini-textbox" style="width: 100%;" minwidth="300" />
            </div>
            <div field="MSort" headeralign="center" vtype="float" width="43px">
                排序
                 <input property="editor" class="mini-spinner" minivalue="1" style="width: 100%;" />
            </div>
            <div field="IsRoot" type="checkboxcolumn" headeralign="center" readonly="true" width="40px">根</div>
            <div field="IsLeaf" type="checkboxcolumn" headeralign="center" readonly="true" width="40px">叶子</div>
            <div field="MLevel" headeralign="center" width="40px" readonly="true">层级</div>
            <div field="IsHasChild" type="checkboxcolumn" headeralign="center" readonly="true" width="40px">
                父级
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">

    mini.parse();
    var tree = mini.get("treegrid1");

    function addRoot() {
        var newNode = { IsRoot: 1, MLevel: 1, IsHasChild: 1, MSort: tree.getData().length + 1 };//getList
        tree.addNode(newNode, "after");
        tree.selectNode(newNode);
        tree.beginEditCell(newNode, "YName");
    }

    function addLeaf() {
        var node = tree.getSelectedNode();
        if (node) {
            var newNode = { IsLeaf: 1, MLevel: node.MLevel + 1, MSort: tree.getChildNodes(node).length + 1 };
            tree.addNode(newNode, "add", node);
            if (node.MLevel > 1)
                tree.updateNode(node, { IsLeaf: 0, IsHasChild: 1 });
            tree.beginEditCell(newNode, "YName");
        }
        else
            top.mini.alert("请选中行");
    }

    function removeNode() {
        var node = tree.getSelectedNode();
        if (node) {
            var fnode = tree.getParentNode(node);//父节点
            tree.removeNode(node);
            if (fnode) {//存在父节点
                if (fnode.MLevel > 1) {
                    if (tree.getChildNodes(fnode).length > 0) {
                        //不作处理
                    }
                    else {
                        tree.updateNode(fnode, { IsLeaf: 1, IsHasChild: 0 });
                    }
                }
            }
        }
        else
            top.mini.alert("请选中行");
    }


    function reloadRow() {
        tree.reload();
    }

    function saveData() {
        tree.validate();
        if (tree.isValid() == false) {
            var error = tree.getCellErrors()[0];
            tree.beginEditCell(error.record, error.column);
            return;
        }

        var added = tree.getChanges("added");
        var jsonAdd = mini.encode(added);

        var modified = tree.getChanges("modified");
        var jsonEdit = mini.encode(modified);

        var removed = tree.getChanges("removed");
        var jsonDel = mini.encode(removed);

        tree.loading("保存中，请稍后......");
        $.ajax({
            url: "Save",
            data: { add: jsonAdd, edit: jsonEdit, del: jsonDel },
            type: "post",
            success: function (text, flag, res) {
                tree.unmask();
                if (res.responseText == "") {
                    tree.reload();
                }
                else
                    top.mini.alert(res.responseText);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                top.mini.alert(errorThrown.responseText);
            }
        });
    }

</script>
